﻿using DICOMViewer.UC;
using DICOMViewer.Utility;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace DICOMDataMasker
{
    public partial class MainForm : Form
    {
        public MainForm()
        {
            InitializeComponent();
        }

        private void cbOutput_CheckedChanged(object sender, EventArgs e)
        {
            this.tbxOutput.Enabled = !cbOutput.Checked;
            this.btnOutputDir.Enabled = !cbOutput.Checked;
            if (this.tbxOutput.Enabled)
            {
                this.tbxOutput.Focus();}
        }

        private void cbOrder_CheckedChanged(object sender, EventArgs e)
        {
            this.tbxOrderNumber.Enabled = cbOrder.Checked;
            if (this.tbxOrderNumber.Enabled)
            {
                this.tbxOrderNumber.Focus();}
        }

        private void tsMenuDir_Click(object sender, EventArgs e)
        {
            FolderBrowserDialog dia = new FolderBrowserDialog();
            dia.Description = "请选择需要上传的文件！";
            dia.RootFolder = Environment.SpecialFolder.Desktop;
            if (dia.ShowDialog() == DialogResult.OK)
            {
                this.tbxFile.Text = dia.SelectedPath;
            }
        }

        private void tsMenyZip_Click(object sender, EventArgs e)
        {
            OpenFileDialog ofDia = new OpenFileDialog();
            ofDia.Title = "请选择需要上传的文件！";
            ofDia.DefaultExt = ".zip";
            ofDia.Filter = "Zip文件|*.zip";
            ofDia.Multiselect = false;
            ofDia.InitialDirectory = Application.StartupPath;
            if (ofDia.ShowDialog() == DialogResult.OK)
            {
                this.tbxFile.Text = ofDia.FileName;
            }
        }

        private void tsMenuExit_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }

        private void btnOutputDir_Click(object sender, EventArgs e)
        {
            FolderBrowserDialog dia = new FolderBrowserDialog();
            dia.Description = "请选择需要上传的文件！";
            dia.RootFolder = Environment.SpecialFolder.Desktop;
            if (dia.ShowDialog() == DialogResult.OK)
            {
                this.tbxOutput.Text = dia.SelectedPath;
            }
        }

        private void btnMark_Click(object sender, EventArgs e)
        {
            ProgressForm form = new ProgressForm(this);
            if (tbxFile.Text.ToLower().EndsWith(".zip"))
            {
                string desPath = 
                    cbOutput.Checked ? 
                    tbxFile.Text.Replace(".zip","_new.zip") : tbxOutput.Text+"\\"+ GetDirName(tbxFile.Text.Replace(".zip", string.Empty));
                ZipFileConverter converter = new ZipFileConverter(tbxFile.Text, desPath, tbxOrderNumber.Text, form);
                converter.OnConverCompleted += Converter_OnConverCompleted;
                converter.beginConvert();
                form.ShowDialog();
            }
            else
            {
                string desPath = cbOutput.Checked ?
                    tbxFile.Text+"\\"+GetDirName(tbxFile.Text)+".zip" :
                    tbxOutput.Text + "\\" + GetDirName(tbxFile.Text) + ".zip";
                string[] files = Directory.GetFiles(tbxFile.Text, "*", SearchOption.AllDirectories);
                ZipFileConverter converter = new ZipFileConverter(files,tbxFile.Text, desPath, tbxOrderNumber.Text, form);
                converter.OnConverCompleted += Converter_OnConverCompleted;
                converter.beginConvert();
                form.ShowDialog();
            }
        }

        string GetDirName(string Dirname)
        {
            return Dirname.Substring(Dirname.LastIndexOf("\\") + 1);
        }
        private void Converter_OnConverCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            this.Invoke(new EventHandler(delegate
            {
                MessageBox.Show("脱敏完成！");
            }));
        }
    }
}
